package com.ruicar.afs.cloud.manage.workflow.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.contract.enums.CaseStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.workflow.entity.WorkTaskAssignInfo;
import com.ruicar.afs.cloud.manage.workflow.entity.WorkTaskCreatedInfo;
import com.ruicar.afs.cloud.manage.workflow.enums.WorkflowTypeEnum;
import com.ruicar.afs.cloud.manage.workflow.service.WorkTaskAssignInfoService;
import com.ruicar.afs.cloud.manage.workflow.service.WorkTaskCreatedInfoService;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.TaskAssignRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.TaskCreateRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.UpdateBizDataRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.TaskAssignResponse;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.TaskCreateResponse;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.UpdateBizDataResponse;
import com.ruicar.afs.cloud.workflow.gwt.processor.AfsWorkFlowNoticeProcessor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 工作流程任务分配
 * @Author xudongdong
 * @Date 2020/07/17
 */
@Component
@Slf4j
@AllArgsConstructor
public class ContractWorkFlowNoticeProcessor implements AfsWorkFlowNoticeProcessor {
    private final WorkTaskAssignInfoService workTaskAssignInfoService;
    private final WorkTaskCreatedInfoService workTaskCreatedInfoService;
    public static CaseStatusEnum CaseStatus;
    public static ProcessStatusEnum processStatus;

    @Override
    public List<String> afsFLowKeys() {
        List list = new ArrayList();
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.INSURANCE_CLAIM));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.INSURANCE_REMNEW));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.INSURANCE_CHANGE_EXIT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTRACT_CANCEL));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTACT_CHANGE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.COST_EXEMTP_BILL));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CUSTINFO_CHANGE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.BANK_CHANGE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.EARLY_SETTLEMENT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTRACT_REISSUR));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.PENALTY_EXEMPT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.COST_EXEMPT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.COLLECTION_REVOKE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTRACT_OVERPAYMENT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.REVOKE_HANDLE));
        return list;
    }

    @Override
    public TaskCreateResponse taskCreated(TaskCreateRequest taskCreateRequest) {
        if (ProcessStatusEnum.approved.name().equals(taskCreateRequest.getTaskMemo())) {
            CaseStatus = CaseStatusEnum.APPROVED;
            processStatus = ProcessStatusEnum.approved;
        } else {
            CaseStatus = CaseStatusEnum.UNDER_REVIEW;
            processStatus = ProcessStatusEnum.underReview;
        }
        log.info("接收到任务创建通知:{}", JSON.toJSONString(taskCreateRequest));
        WorkTaskCreatedInfo workTaskCreatedInfo = workTaskCreatedInfoService.getOne(Wrappers.<WorkTaskCreatedInfo>query().lambda()
                .eq(WorkTaskCreatedInfo::getBizDataId, taskCreateRequest.getBizDataId()));
        if (EmptyUtils.isEmpty(workTaskCreatedInfo)) {
            workTaskCreatedInfo = new WorkTaskCreatedInfo();
            BeanUtils.copyProperties(taskCreateRequest, workTaskCreatedInfo);
            workTaskCreatedInfoService.save(workTaskCreatedInfo);
        } else {
            BeanUtils.copyProperties(taskCreateRequest, workTaskCreatedInfo);
            workTaskCreatedInfoService.update(workTaskCreatedInfo, Wrappers.<WorkTaskCreatedInfo>lambdaUpdate()
                    .eq(WorkTaskCreatedInfo::getBizDataId, taskCreateRequest.getBizDataId()));
        }
        TaskCreateResponse response = new TaskCreateResponse();
        response.setSuccess(true);
        return response;
    }

    @Override
    public TaskAssignResponse taskAssigned(TaskAssignRequest taskAssignRequest) {
        log.info("接收到任务分配通知:{}", JSON.toJSONString(taskAssignRequest));
        WorkTaskAssignInfo workTaskAssignInfo = new WorkTaskAssignInfo();
        BeanUtils.copyProperties(taskAssignRequest, workTaskAssignInfo);
        workTaskAssignInfo.setGwtUserId(taskAssignRequest.getId());
        workTaskAssignInfoService.save(workTaskAssignInfo);
        TaskAssignResponse response = new TaskAssignResponse();
        response.setSuccess(true);
        return response;
    }

    @Override
    public UpdateBizDataResponse updateBizData(UpdateBizDataRequest updateBizDataRequest) {
        log.info("接收到更新业务通知:{}", JSON.toJSONString(updateBizDataRequest));
        UpdateBizDataResponse response = new UpdateBizDataResponse();
        response.setSuccess(true);
        return response;
    }
}
